<!--
 * @Author: 王鹏 2911783749@qq.com
 * @Date: 2022-10-23 18:34:43
 * @LastEditors: 王鹏 2911783749@qq.com
 * @LastEditTime: 2022-10-24 15:55:20
 * @FilePath: \v_shop\code\src\components\children\Goods.vue
 * @Description: 商品列表页
-->
<!--  -->
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <!-- 搜索/添加 -->
      <el-row :gutter="20">
        <el-col :span="16"
          ><div class="grid-content bg-purple">
            <el-input placeholder="请输入内容" class="input-with-select" v-model="queryInfo.query">
              <el-button slot="append" icon="el-icon-search" @click="searchGoodsList"></el-button>
            </el-input>
            <el-button type="primary" @click="showGoodsModule"
              >添加商品</el-button
            >
          </div></el-col
        >
        <el-col :span="8"><div class="grid-content bg-purple"></div></el-col>
      </el-row>
      <!-- 表格 -->
      <el-table :data="goodsList" border style="width: 100%" max-height="400">
        <el-table-column fixed label="#" type="index" align="center">
        </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" align="center">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格" align="center">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" align="center">
        </el-table-column>
        <el-table-column prop="add_time" label="创建时间" align="center">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="editGoods(scope.$index, scope.row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="delGoods(scope.$index, scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 弹窗 -->
      <GoodsDialog
        :dialog="dialog"
        :goodsForm="goodsForm"
        @update="getGoodsList"
      />
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { goods_list, del_goods } from "../../http/api";
import GoodsDialog from "../../views/GoodsDialog.vue";
export default {
  name: "Goods",
  data() {
    return {
      goodsList: [],
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      total:0,
      goodsForm: {
        goods_name: "",
        goods_cat: "1,2,3",
        goods_price: "",
        goods_number: "",
        goods_weight: "",
        pics: [],
      },
      dialog: {
        show: false,
        title: "编辑商品",
        option: "edit",
      },
    };
  },

  components: { GoodsDialog },

  computed: {},

  mounted() {
    this.getGoodsList();
  },

  methods: {
    getGoodsList() {
      goods_list(this.queryInfo).then((res) => {
        console.log(res);
        this.goodsList = res.data.data.goods;
        this.total=res.data.data.total
      });
    },
    searchGoodsList(){
      goods_list(this.queryInfo).then((res) => {
        console.log(res);
        this.goodsList = res.data.data.goods;
        this.$message.success(res.data.meta.msg)
      });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.queryInfo.pagesize=val
      this.getGoodsList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.queryInfo.pagenum=val
      this.getGoodsList()
    },
    delGoods(index, value) {
      del_goods(value).then((res) => {
        this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            console.log(res);
            this.$message.success(res.data.meta.msg);
            this.getGoodsList();
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      });
    },
    showGoodsModule() {
      this.goodsForm = {
        goods_name: "",
        goods_cat: "1,2,3",
        goods_price: "",
        goods_number: "",
        goods_weight: "",
        pics: [],
      };
      this.dialog = {
        show: true,
        title: "添加商品",
        option: "add",
      };
    },
    editGoods(index, value) {
      this.goodsForm = {
        goods_id: value.goods_id,
        goods_name: value.goods_name,
        goods_cat: "1,2,3",
        goods_price: value.goods_price,
        goods_number: value.goods_number,
        goods_weight: value.goods_weight,
      };
      this.dialog = {
        show: true,
        title: "编辑商品",
        option: "edit",
      };
    },
  },
  watch: {},
  mixins: [],
};
</script>
<style  scoped>
.el-card {
  margin-top: 20px;
}
.input-with-select {
  width: 450px;
  margin-right: 20px;
}
</style>